package com.dykj.fireFighting.web.base.analysis.controller;

import com.dykj.fireFighting.api.base.analysis.dto.QueryAnalysisDto;
import com.dykj.fireFighting.api.base.analysis.entity.WarningRecovery;
import com.dykj.fireFighting.api.base.analysis.vo.ComprehensiveResultFLVo;
import com.dykj.fireFighting.api.base.dev.vo.RateVo;
import com.dykj.fireFighting.api.base.devHisResult.service.IDevHisResultService;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.utils.DateTimeCutUtil;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: zly
 * @Description : 研判分析
 * @Date: 2022-04-14
 */
@RestController
@RequestMapping("warningrecovery")
public class WarningRecoveryController {

    @DubboReference
    private IDevHisResultService devHisResultService;
    @DubboReference
    private IEnterpriseService enterpriseService;

    @AutoLog(value = "社会单位警情研判-研判数据")
    @ApiOperation(value = "社会单位警情研判-研判数据", notes = "社会单位警情研判-研判数据")
    @RequestMapping("/getList")
    public CommonResult<PageVo<WarningRecovery>> getList(QueryAnalysisDto queryAnalysis){
        LoginUserVo logUser = ShiroUtil.getUser();
       queryAnalysis.setCenterUnitId(logUser.getEnterpriseId().toString());
        PageVo<WarningRecovery> pageVo=devHisResultService.getList(queryAnalysis);
        return CommonResult.success(pageVo,"查询成功");
    }

    @AutoLog(value = "社会单位警情研判-图表")
    @ApiOperation(value = "社会单位警情研判-图表", notes = "社会单位警情研判-图表")
    @RequestMapping("/getChartDate")
    public CommonResult getChartDate(QueryAnalysisDto queryAnalysis) throws ParseException {
        LoginUserVo logUser = ShiroUtil.getUser();
        //queryAnalysis.setCenterUnitId(logUser.getEnterpriseId().toString());

        List<WarningRecovery> list=devHisResultService.getChartDate(queryAnalysis);

        /**
         * type取值1 年，2 季度 ，3 月，4 日
         * 补零
         */
        List<String> resDateNameList = list.stream().map(WarningRecovery -> {
            return WarningRecovery.getDateDisplay() ;
        }).collect(Collectors.toList());
       List<String> newList = new ArrayList<>();
        switch (queryAnalysis.getType()){
            case 1:
                List<String> yearList = DateTimeCutUtil.cutByType(queryAnalysis.getBeginTime(), queryAnalysis.getEndTime(), "year");
                newList=DateTimeCutUtil.subList2(yearList,resDateNameList);
                break;
            case 2:
                List<String> quarterList = DateTimeCutUtil.cutByType(queryAnalysis.getBeginTime(), queryAnalysis.getEndTime(), "quarter");
                newList=DateTimeCutUtil.subList2(quarterList,resDateNameList);
                break;
            case 3:
                List<String>  monthList = DateTimeCutUtil.cutByType(queryAnalysis.getBeginTime(), queryAnalysis.getEndTime(), "month");
                newList=DateTimeCutUtil.subList2(monthList,resDateNameList);
                break;
            case 4:
                List<String> dayList = DateTimeCutUtil.cutByType(queryAnalysis.getBeginTime(), queryAnalysis.getEndTime(), "day");
                newList=DateTimeCutUtil.subList2(dayList,resDateNameList);
                break;
        }
        ArrayList<WarningRecovery> addList = new ArrayList<>();
        for (int i = 0; i <newList.size() ; i++) {
            WarningRecovery warningRecovery = new WarningRecovery();
            warningRecovery.setDateDisplay(newList.get(i));
            warningRecovery.setChuzhiRate("0");
            warningRecovery.setFuheRate("0");
            addList.add(warningRecovery);
        }
        list.addAll(addList);
        list = list.stream().sorted(Comparator.comparing(WarningRecovery::getDateDisplay))
                .collect(Collectors.toList());

        return CommonResult.success(list,"查询成功");
    }




}
